package com.hz.controller;

import com.hz.pojo.User;
import com.hz.service.UserService;
import com.hz.utils.MassageJson;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/loginUser")
    @ResponseBody
    public MassageJson logingUser(String userName, String userPassWord) {


        try {
            return userService.userLogin(userName, userPassWord);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new MassageJson<User>("400", "接口异常", null);
    }

    @GetMapping("/findUser")
    @ResponseBody
    public MassageJson<User> findUserByToken(@RequestHeader String token) {

        try {
            return userService.findeUserByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return new MassageJson<User>("400", "接口异常", null);
    }


    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/indexinfo")
    public String index() {
        return "index";
    }

    @RequestMapping("/out")
    @ResponseBody
    public MassageJson<String> out(String token) {

        try {
            return userService.out(token);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    @RequestMapping("/findList")
    @ResponseBody
    public MassageJson findList() {
        MassageJson massageJson = userService.findUserList();


        return massageJson;
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public MassageJson insertUser(@RequestBody User user) {
        MassageJson massageJson = userService.insertUser(user.getUserName(),user.getUserPassword());

        return massageJson;
    }

    @RequestMapping("/daoChu")
    @ResponseBody
    public void daoChu(HttpServletResponse response) {
        userService.daochu(response);
    }


    @RequestMapping("/daoRu")
    @ResponseBody
    public void daoRu(@RequestParam("file") MultipartFile file)  throws IOException {
        userService.daoRu(file);
    }
    @RequestMapping("/test")
    @ResponseBody
    public void test(){
        userService.test();
    }
}
